//
//  FillDataViewController.m
//  i读
//
//  Created by 123 on 15/10/20.
//  Copyright © 2015年 123. All rights reserved.
//

#import "FillDataViewController.h"
//#import "RequestManager.h"
#import "UserModel.h"
//#import "AppPreference.h"
#import "ViewController.h"
@interface FillDataViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate,UIActionSheetDelegate>
{
    UIActionSheet *myActionSheet;
    NSString* filePath;
}
@end

@implementation FillDataViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTextField];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -          -------- 输入框常规设置

- (void)setTextField{

    //占字符颜色
    [_nameField setValue:[[UIColor whiteColor] colorWithAlphaComponent:0.7] forKeyPath:@"_placeholderLabel.textColor"];
    [_signField setValue:[[UIColor whiteColor] colorWithAlphaComponent:0.7] forKeyPath:@"_placeholderLabel.textColor"];
    [_jobField setValue:[[UIColor whiteColor] colorWithAlphaComponent:0.7] forKeyPath:@"_placeholderLabel.textColor"];
    [_loveField setValue:[[UIColor whiteColor] colorWithAlphaComponent:0.7] forKeyPath:@"_placeholderLabel.textColor"];
    //占字符字体大小
    [_nameField setValue:[UIFont boldSystemFontOfSize:12] forKeyPath:@"_placeholderLabel.font"];
    [_signField setValue:[UIFont boldSystemFontOfSize:12] forKeyPath:@"_placeholderLabel.font"];
    [_jobField setValue:[UIFont boldSystemFontOfSize:12] forKeyPath:@"_placeholderLabel.font"];
    [_loveField setValue:[UIFont boldSystemFontOfSize:12] forKeyPath:@"_placeholderLabel.font"];
    
    [_manBtn addTarget:self action:@selector(manBtnCilck:) forControlEvents:UIControlEventTouchUpInside];
    [_womanBtn addTarget:self action:@selector(womanBtnCilck:) forControlEvents:UIControlEventTouchUpInside];
    self.sexLabel = [[UILabel alloc]init];
    self.sexLabel.text = @"1";
}

#pragma mark -          -------- 设置性别点击事件

// 性别按钮点击事件
- (void)manBtnCilck:(UIButton *)sender{
    [_manBtn setBackgroundImage:[UIImage imageNamed:@"fillout_checkbox_00_s"] forState:UIControlStateNormal];
    [_womanBtn setBackgroundImage:[UIImage imageNamed:@"fillout_checkbox_00_n"] forState:UIControlStateNormal];
    self.sexLabel.text = @"1";
}

- (void)womanBtnCilck:(UIButton *)sender{
    [_manBtn setBackgroundImage:[UIImage imageNamed:@"fillout_checkbox_00_n"] forState:UIControlStateNormal];
    [_womanBtn setBackgroundImage:[UIImage imageNamed:@"fillout_checkbox_00_s"] forState:UIControlStateNormal];
    self.sexLabel.text = @"0";
}

#pragma mark -          -------- 返回、跳过、提交事件

//返回点击事件
- (IBAction)backBtn:(UIButton *)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}
//跳过点击事件
- (IBAction)skipBtn:(UIButton *)sender {
    ViewController *homeView = [[ViewController alloc]init];
    [self.navigationController pushViewController:homeView animated:YES];
}
// 完成提交事件
//- (IBAction)commitBtn:(UIButton *)sender {
//    
//    /****接口****/
//   [RequestManager modifyUserInfoWithUserID:[AppPreference defaultPreference].currentUserId nickName:self.nameField.text gender:self.sexLabel.text signature:self.signField.text occupation:self.jobField.text hobby:self.loveField.text compaletionHandler:^(UserModel *userInfo, NSString *reCode, NSString *reMsg){
//        if ([reCode isEqualToString:@"0"]) {
//            [[AppPreference defaultPreference] loginWithUser:userInfo];
//            ViewController *homeView = [[ViewController alloc]init];
//            [self.navigationController pushViewController:homeView animated:YES];
//        }
//
//    }];
//    
//}

#pragma mark -          -------- 取消键盘第一响应

// 点击任意位置是键盘消失
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    UITouch *touch = [touches anyObject];
    
    if (![touch.view isKindOfClass: [UITextField class]] || ![touch.view isKindOfClass: [UITextView class]]) {
        
        [self.view endEditing:YES];
        
    }
    
}


#pragma mark -            -------- 上传头像图片或拍照上传

/******open camera file*/
-(IBAction)openMenu:(id)sender
{
    //在这里呼出下方菜单按钮项
    myActionSheet = [[UIActionSheet alloc]
                     initWithTitle:nil
                     delegate:self
                     cancelButtonTitle:@"取消"
                     destructiveButtonTitle:nil
                     otherButtonTitles: @"打开照相机", @"从手机相册获取",nil];
    
    [myActionSheet showInView:self.view];
    
    
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    //呼出的菜单按钮点击后的响应
    if (buttonIndex == myActionSheet.cancelButtonIndex)
    {
        NSLog(@"取消");
    }
    
    switch (buttonIndex)
    {
        case 0:  //打开照相机拍照
            [self takePhoto];
            break;
            
        case 1:  //打开本地相册
            [self LocalPhoto];
            break;
    }
}

//开始拍照
-(void)takePhoto
{
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate =self;
        //设置拍照后的图片可被编辑
        picker.allowsEditing = YES;
        picker.sourceType = sourceType;
        
        [self presentViewController:picker animated:YES completion:nil];
    }else
    {
        NSLog(@"模拟器中无法打开照相机,请在真机中使用");
    }
}

//打开本地相册
-(void)LocalPhoto
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    //设置选择后的图片可被编辑
    picker.allowsEditing = YES;
    [self presentViewController:picker animated:YES completion:nil];
    
}

//当选择一张图片后进入这里
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{
    
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];




    //当选择的类型是图片
    if ([type isEqualToString:@"public.image"])
    {
        //先把图片转成NSData
       UIImage* image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
        
        NSData *data;
        if (UIImagePNGRepresentation(image) == nil)
        {
            data = UIImageJPEGRepresentation(image, 3);
        }
        else
        {
            data = UIImagePNGRepresentation(image);
        }
        
        [_iconBtn setBackgroundImage:image  forState:UIControlStateNormal];
         [picker dismissViewControllerAnimated:YES completion:nil];
        
        //  上传图片
        /****接口****/
//        [RequestManager uploadHeadImgWithUserId:[AppPreference defaultPreference].currentUserId imgFile:data completionHandler:^(NSString *headImg, NSString *reCode, NSString *reMsg){
//            if ([reCode isEqualToString:@"0"]) {
//                UserModel *user = [AppPreference loginUser];
//                user.headImg = headImg;
//                  [[AppPreference defaultPreference]loginWithUser:user];
//            }
//        }];
    }
    
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    NSLog(@"您取消了选择图片");
    [picker dismissViewControllerAnimated:YES completion:nil];
}




@end
